var chooseLocation = requirePlugin('chooseLocation');
let app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_default:0,
    areaInfo: '',
    province_id:'',
    city_id:'',
    area_id:'',
    showadd:true,
    showaddress:false,
    formto: 0,
    true_name_show:false,
    mob_phone_show:false,
    address_show:false,
    true_name: '',
    mob_phone: '',
    address: '',
	genderIndex: 0,
	genders: ['先生', '女士'],
	from:'',
	navigationAddress: '',
	latitude: '',
	longitude: '',
  },
onShow() {
		const selectedLocation = chooseLocation.getLocation();
		if (selectedLocation) {
			this.setData({ 
				navigationAddress: selectedLocation.address,
				latitude: selectedLocation.latitude,
				longitude: selectedLocation.longitude,
			});
		  
		}
	},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    new app.ToastPannel();
    var that = this
    var fromto = options.fromto
    if (fromto != 1) {
      fromto = 0
    }
    this.setData({
      fromto: fromto
    })
    that.address(0)
  },
 
  formSubmit: function (e) {
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var true_name = e.detail.value.true_name
    var mob_phone = e.detail.value.mob_phone
    var area_info = that.data.areaInfo
    var address = e.detail.value.address
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (true_name.length == 0) {
      that.show('请输入收货人姓名')
    } else if (mob_phone.length == 0){
      that.show('请输入收货人手机号码')
    } else if (!myreg.test(mob_phone)){
      that.show('手机号格式有误')
    }else if (area_info.length == 0) {
      that.show('请输入所在地区')
    } else if (address.length == 0) {
      that.show('请输入详细地址')
    } else if (that.data.navigationAddress == '') {
      that.show('请选择导航地址')
    } else {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_User&met=addAddressInfo&typ=json',
        data: { k: key, 
		u: uid, 
		user_address_contact: true_name, 
		user_address_phone: mob_phone, 
		province_id: that.data.province_id, 
		city_id: that.data.city_id, 
		area_id: that.data.area_id, 
		user_address_address: address, 
		address_area: area_info, 
		user_address_default: that.data.is_default,
		
		navigationAddress: that.data.navigationAddress,
		latitude: that.data.latitude,
		longitude: that.data.longitude  },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        }, // 设置请求的 header
        success: function (res) {
         
          if(res.data.status == 200){
            that.show('添加成功')
			wx.navigateBack()
            // if(that.data.fromto == 1){
            //   wx.redirectTo({
            //     url: '../address_select/address_select',
            //   })
            // }else{
            //   wx.redirectTo({
            //     url: '../address_list/address_list',
            //   })
            // }
            
          }else{
            that.show('添加失败')
          }
          
          
        },
        fail: function (res) {
        
        }
      })
    }
    
  },
  //设置默认发货地址
  switch_change: function (e) {
    var is_default = e.detail.value
    if (is_default) {
      this.setData({
        is_default: 1
      })
    }
  },
// 性别选择变化
	  genderChange(e) {
		this.setData({ genderIndex: e.detail.value[0] })
	  },
	  navigationAddress: function() {
	  		  const key = "DEHBZ-VTH3L-ZU7PV-MHG4B-IHHTE-FVB5G"; // 替换为实际Key
	  		  const referer = "铧佗帮护";
	  		  wx.navigateTo({
	  		    url: `plugin://chooseLocation/index?key=${key}&referer=${referer}`
	  		  });
	  },
  //选择地区
  area_info:function(){
    var that = this;
    that.setData({
      showaddress: (!that.data.showaddress),
      showadd: (!that.data.showadd)
    })
    if (that.data.showaddress == true){
      that.address(0)
    }
  },

  //获取一级地区
  address: function (e) {
    var that = this
    if (e == 0 ) {
      var id = 0
      var areaInfo = ''
      that.setData({
        areaInfo: areaInfo
      })
     
    } else {
      var id = e.currentTarget.dataset.id
    }
   
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Base_District&met=district&typ=json',
      method: 'GET',
      data: {
        pid: id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.status == 200) {
          var areaInfo = that.data.areaInfo
          if (e != 0) {
            if (e.currentTarget.dataset.level == 0) {
              areaInfo = areaInfo + " " + e.currentTarget.dataset.name
              that.setData({
                province_id:id
              })
            } else if (e.currentTarget.dataset.level == 0) {
              areaInfo = areaInfo + " " + e.currentTarget.dataset.name
              that.setData({
                city_id: id
              })
             
            } else if (e.currentTarget.dataset.level == 0) {
              areaInfo = areaInfo + " " + e.currentTarget.dataset.name
              that.setData({
                area_id: id
              })
              
            }
            that.setData({
              areaInfo: areaInfo
            })
            if (res.data.data.items.length == 0 || e.currentTarget.dataset.level == 3) {
              that.setData({
                showaddress: (!that.data.showaddress),
                showadd: (!that.data.showadd)
              })
            }
          }

          that.setData({
            address_list: res.data.data.items,
          })
        }
      }
    })
  },
  //收货人
  true_name:function(e){
    var that = this
    if (e.detail.value) {
      that.setData({
        true_name_show: true,
        true_name: e.detail.value
      })
    }else{
      that.setData({
        true_name_show: false
      })
    }
  },
  //手机号码
  mob_phone:function(e){
    var that = this
    if (e.detail.value) {
      that.setData({
        mob_phone_show: true,
        mob_phone: e.detail.value
      })
    }else{
      that.setData({
        mob_phone_show: false
      })
    }
  },
  //详细地址
  addressinput:function(e){
    var that = this
    if(e.detail.value){
      that.setData({
        address_show: true,
        address: e.detail.value
      })
    }else{
      that.setData({
        address_show:false
      })
    }
    
  },

  //收货人
  true_name_clear: function (e) {
    var that = this
    that.setData({
      true_name_show: false,
      true_name: ''
    })
  },
  //手机号码
  mob_phone_clear: function () {
    var that = this
    that.setData({
      mob_phone_show: false,
      mob_phone: ''
    })
  },
  //详细地址
  address_clear: function (e) {
    var that = this
    that.setData({
      address_show: false,
      address: ''
    })

  }

})